
#include"compile_run.hpp"
#include"../comm/httplib.h"
using namespace ns_compiler_and_run;
using namespace httplib;

void Usage(std::string proc)
{
  std::cerr<<"Usage"<<"./compile_server  "<<"prot"<<std::endl;
}
//编译服务随时会被多个人请求,必须保证传递上来的code形成源文件的时候具有唯一性
// ./compile_server port
int main(int argc,char *argv[])
{
   //通过http 让client上传一个json string
   // in_json{"code","#include--","input","cpu_limit","men_limit"};
   // out_json{"status","reason","stdout","stderr",""

   //模拟客户请求
   /*
   std::string in_json;
   Json::Value in_value;
   //R "()" 里面保持原来的字符,不要转义
   in_value["code"]=R"(#include<iostream> 
   int main(){ std::cout<<"hello"<<std::endl;return 0;}
   )";
   in_value["input"]="";
   in_value["cpu_limit"]=1;
   in_value["mem_limit"]=10240*3;

   Json::FastWriter writer;
   in_json =writer.write(in_value); //json值对象转为json字符串

   //std::cout<<in_json<<std::endl;
   std::string out_json; //给客户端返回的
   CompileAndRun::Start(in_json,&out_json);
   std::cout<<out_json<<std::endl;
   */
  
   if(argc!=2)
   {
      Usage(argv[0]);
      return 1;
   }
   Server svr;
   //网络服务
   svr.Post("/compile_and_run",[](const Request &req,Response &resp){
     //用户请求的是json
     std::string in_json=req.body;
     std::string out_json;
     if(!in_json.empty())
     {
         CompileAndRun::Start(in_json,&out_json);
         resp.set_content(out_json,"application/json;charset=utf-8");
     }
   });
   svr.listen("0.0.0.0",atoi(argv[1])); //启动http服务


   return 0;
}